{
 "metadata": {
  "name": "",
  "signature": "sha256:507afb0be4840226c86bfe522c9edc5550cdebb1c2b8bcce03ac9f118e89be15"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# Open ORIGEN data generation\n",
      "\n",
      "This notebook generates data for the ORIGEN v2.2 TAPE9.INP file. The data format is described in http://web.ornl.gov/info/reports/1980/3445605762840.pdf on pages 63-66 in the text. "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "import pyne\n",
      "from pyne import nucname\n",
      "from pyne import data"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 38
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Read data from nuc_data\n",
      "This uses the ENSDF data stored in nuc_data.h5"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import tables as tb\n",
      "def getnuc(nuc = 430990001):\n",
      "    \"\"\"\n",
      "    This computes ORIGEN data based on ENSDF data. \n",
      "    FIXME: calculate B- and EC decays that end in metastable state after gamma transition\n",
      "\n",
      "    Parameters\n",
      "    ----------\n",
      "    nid : nuc_id\n",
      "        a valid string or int that can be converted into a nuc_id\n",
      "    meta_t : float\n",
      "        minimum lifetime of metastable state (in seconds) to be included (default 1.0 s)\n",
      "\n",
      "    Returns\n",
      "    -------\n",
      "    nuc_id : int\n",
      "        nuc_id of parent\n",
      "    t12 : float\n",
      "        half life of parent in seconds\n",
      "    fb : float\n",
      "        percent of B- decays per decay of parent \n",
      "    fbx : float\n",
      "        percent of B- decays that end in metastable state\n",
      "    fsf : float\n",
      "        percent of spontaneous fission decays per decay of parent\n",
      "    fpec : float\n",
      "        percent of B+ and EC decays per decay of parent\n",
      "    fpecx : float\n",
      "        percent of B+ and EC decays that end in metastable state\n",
      "    fa : float\n",
      "        percent of A decays per decay of parent\n",
      "    fn : float\n",
      "        percent of B- + neutron decays per decay of parent\n",
      "    fit : float\n",
      "        percent of internal transition decays per decay of parent\n",
      "    \"\"\"\n",
      "    if nuc % 10000 > 0:\n",
      "        nuc = pyne.nucname.id_to_state_id(nuc)\n",
      "    t12 = np.inf\n",
      "    f = tb.open_file(pyne.nuc_data)\n",
      "    a = None\n",
      "    bm =  None\n",
      "    sf = None\n",
      "    ec = None\n",
      "    ecx = None\n",
      "    it = None\n",
      "    bmn = None\n",
      "    bmx = None\n",
      "    for item in f.root.decay.level_list.where('nuc_id == ' + str(nuc)):\n",
      "        if item['rx_id'] != 0:\n",
      "            if item['rx_id'] == 36565:\n",
      "                sf = item['branch_ratio']\n",
      "            elif item['rx_id'] == 1089:\n",
      "                a = item['branch_ratio']\n",
      "            elif item['rx_id'] == 4130566254:\n",
      "                bm = item['branch_ratio']\n",
      "            elif item['rx_id'] == 35974:\n",
      "                ec = item['branch_ratio']\n",
      "            elif item['rx_id'] == 36125:\n",
      "                it = item['branch_ratio']\n",
      "            elif item['rx_id'] == 1355894015:\n",
      "                bmn = item['branch_ratio']\n",
      "            #print item['rx_id'],rxname.name(item['rx_id']), item['branch_ratio']\n",
      "        else:\n",
      "            t12 = item['half_life']\n",
      "    if bm is not None:\n",
      "        beta_states = data.beta_child_byparent(nuc)\n",
      "        beta_intens = data.beta_intensity(nuc)\n",
      "        bmx = 0.0\n",
      "        for index, state in enumerate(beta_states):\n",
      "            try:\n",
      "                pyne.nucname.state_id_to_id(state)\n",
      "            except RuntimeError:\n",
      "                pass\n",
      "            else:\n",
      "                bmx = bmx + beta_intens[index]\n",
      "        bmx = bmx * data.decay_beta_branch_ratio(nuc,(state // 10000) * 10000)[0]\n",
      "        bmx = bmx/bm*100.0\n",
      "    if ec is not None:\n",
      "        ec_states = data.ecbp_child_byparent(nuc)\n",
      "        ec_intens = data.ec_intensity(nuc)\n",
      "        bp_intens = data.beta_plus_intensity(nuc)\n",
      "        ecx = 0.0\n",
      "        for index, state in enumerate(ec_states):\n",
      "            try:\n",
      "                pyne.nucname.state_id_to_id(state)\n",
      "            except RuntimeError:\n",
      "                pass\n",
      "            else:\n",
      "                ecx = ecx + ec_intens[index] + bp_intens[index]\n",
      "        ecx = ecx * data.decay_beta_branch_ratio(nuc,(state // 10000) * 10000)[0]\n",
      "        ecx = ecx/ec*100.0\n",
      "    return nuc, t12, bm, bmx, sf, ec, ecx, a, bmn, it"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 40
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "getnuc(551370000)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 41,
       "text": [
        "(551370000, 949252608.0, 100.0, 94.7, None, None, None, None, None, None)"
       ]
      }
     ],
     "prompt_number": 41
    }
   ],
   "metadata": {}
  }
 ]
}
